package com.clinic.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.clinic.entity.Orders;
import com.clinic.service.OrdersService;
import com.clinic.vo.ActionResult;
import com.clinic.vo.OrdersVo;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.ietf.jgss.Oid;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Orders)表控制层
 *
 * @author makejava
 * @since 2020-11-12 15:55:19
 */
@RestController
@RequestMapping("orders")
public class OrdersController {
    /**
     * 服务对象
     */
    @Resource
    private OrdersService ordersService;

    //t添加订单
    @PostMapping("/add")
    public ActionResult addAll(@RequestBody String orders){
        String s=JSONUtils.toJSONString(orders);
        JSONObject jsonObject = JSONObject.parseObject(orders);
        JSONArray orders1 = (JSONArray) jsonObject.get("ordrs");
        List<OrdersVo> list =JSONArray.parseArray(orders1.toString(),OrdersVo.class);
        for (OrdersVo ordersVo:list){
            System.out.println(ordersVo);
        }
        if (list!=null){
        ordersService.addOrder((OrdersVo) list);
        return new ActionResult(200,"添加失败",list);
        }
        return new ActionResult(500,"添加失败",null);
    }

    //根据id查询单个
    @GetMapping("/find")
    @CrossOrigin
    public ActionResult findOrdersById(int oid){
        List<Orders> orderByPid = ordersService.findOrderByPid(oid);
        if (orderByPid!=null){
            return new ActionResult(200,"查询成功",orderByPid);
        }
            return new ActionResult(500,"查询失败",null);
    }

}